package com.erp.module.scanBaomingxi

import androidx.appcompat.app.AlertDialog
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.myAdapter.BaomingxiRvAdapter
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_scan_baomingxi.*
import kotlinx.android.synthetic.main.dialog_photo_long.*
import kotlinx.android.synthetic.main.header_knoya.*
import org.json.JSONException
import org.json.JSONObject
import java.util.ArrayList

/**
 * Created by Administrator on 2019/3/22/022.
 * 作者：刘洋
 */
class ScanActivity  : BaseActivity(), View.OnClickListener {
    lateinit var adapter : BaomingxiRvAdapter
    lateinit var M : scanBaomingxiM
    lateinit var mlist: MutableList<baomingxiBean>
    var updatedialog: AlertDialog? = null
    var scan = ""
    override fun getLayoutId(): Int {
        return R.layout.activity_scan_baomingxi
    }

    override fun initCreate() {
        scan = intent.getStringExtra("scan")
        mlist = ArrayList()
        M = scanBaomingxiM()
        adapter = BaomingxiRvAdapter(mlist, this)
        RvScanBaomingxi.setLayoutManager(LinearLayoutManager(this))
        RvScanBaomingxi.adapter = adapter
        tv_header_title.text = scan
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        getdata()
    }

    fun getdata(){
        updatedialog = Myutil.showupdatedialog(this, "正在获取包明细", false)
        M.getZhuanMaiDian(this,scan,object : OnUserDataInterface{
            override fun onNot(throwable: String?) {
                updatedialog?.dismiss()
            }

            override fun onUserData(data: String?) {
                updatedialog?.dismiss()
                if (data != null) {
                    jsondata(data)
                }
            }

        })
    }

    fun jsondata(data : String){
        if(data==null){
            return
        }
        try {
            mlist.clear()
            val j = JSONObject(data)
            var js = JsonUtil.getjsvolue(j,"msg")
            for (i in -0 until js.length()) {
                val bean = baomingxiBean()
                val jo = js.get(i) as JSONObject
                bean.name = JsonUtil.getvolue(jo, "name")
                bean.setLength(JsonUtil.getvolue(jo, "length"))
                bean.setWidth(JsonUtil.getvolue(jo, "width"))
                bean.setHeight(JsonUtil.getvolue(jo, "height"))
                bean.count = JsonUtil.getvolue(jo, "count")
                mlist.add(bean)
            }
        } catch (e: JSONException) {
            e.printStackTrace()

        }
        Log.d("TAAAH","sss"+mlist.count())
        adapter.notifyDataSetChanged()
    }

    override fun onClick(v: View?) {
        when(v){
            iv_header_left -> finish()
        }
    }
}